﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ODS.WebService.DeviceFunctions;
using ODS.Model;
using ODS.Utils;
using ODS.Config;
using ODS.WebService.DeviceCommand;
using System.Threading.Tasks;

namespace ODS.UserControls
{
    public partial class SettingDevicePage : UserControl
    {
        public PhoneApplicationPage parrentPage;
        public bool IsScanQRCodeDeviceIMEI = false;

        public SettingDevicePage()
        {
            InitializeComponent();

            setDataForView();
        }

        public void setDataForView()
        {
            tb_deviceName.MyText = App.currentDeviceModel.VehicleName;
            tb_deviceIMEI.MyText = App.currentDeviceModel.DeviceIMEI;
            tb_deviceNumber.MyText = App.currentDeviceModel.DevicePhoneNumber;
        }

        public void SetSubscriptionCode(String deviceIMEI)
        {
            tb_deviceIMEI.MyText = deviceIMEI;
        }

        public async Task Apply()
        {
            if (ValidateInput())
            {
                DeviceModel tempDeviceModel = new DeviceModel();
                tempDeviceModel.DeviceIMEI = tb_deviceIMEI.MyText;
                tempDeviceModel.VehicleName = tb_deviceName.MyText;
                tempDeviceModel.DevicePhoneNumber = tb_deviceNumber.MyText;

                String message = "";
                bool updateOK = false;
                InfoResult result_modifyDevice = await ModifyDeviceService.ModifyDevice(tempDeviceModel);
                if (result_modifyDevice.IsSuccess)
                {
                    if ((String)result_modifyDevice.ReturnedObject == Constants.SUCCESS)
                    {
                        String bodyURL = String.Format("<commandInfo><commandType>16</commandType><parameter><name>value</name><value>{0}</value></parameter></commandInfo>", tb_odometer.MyText);
                         InfoResult result_requestCommand = await RequestCommandService.RequestCommand(tempDeviceModel.DeviceIMEI, 16, bodyURL);
                         if (result_requestCommand.IsSuccess)
                         {
                             if ((String)result_requestCommand.ReturnedObject == Constants.SUCCESS)
                             {
                                 App.currentDeviceModel.DeviceIMEI = tempDeviceModel.DeviceIMEI;
                                 App.currentDeviceModel.DevicePhoneNumber = tempDeviceModel.DevicePhoneNumber;
                                 App.currentDeviceModel.VehicleName = tempDeviceModel.VehicleName;

                                 message = "Update device is successfully";
                                 updateOK = true;
                                 
                             }
                             else
                             {
                                 message = result_requestCommand.ReturnedObject as String;
                             }
                         }
                         else
                         {
                             message = result_requestCommand.Error;
                         }
                    }
                    else
                    {
                        message = (String)result_modifyDevice.ReturnedObject;
                    }
                }
                else
                {
                    message = result_modifyDevice.Error;
                }

                MessageBox.Show(message);

                if (updateOK)
                {
                    App.BackToMainPage(parrentPage);
                }
            } 
        
        }

        public bool ValidateInput()
        {
            if (tb_deviceName.MyText == "")
            {
                MessageBox.Show("Device Name is empty, please try again.");
                return false;
            }

            if (tb_deviceNumber.MyText == "")
            {
                MessageBox.Show("Device number is empty, please try again.");
                return false;
            }

            if (tb_deviceNumber.MyText.Length > 15)
            {
                MessageBox.Show("Device number:1-15 symbols, please try again.");
                return false;
            }

            if (tb_odometer.MyText == "")
            {
                MessageBox.Show("Odometer is empty, please try again.");
                return false;
            }

            return true;
        }

        private void btn_scanQRCodeDeviceIMEI_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            IsScanQRCodeDeviceIMEI = true;
            this.parrentPage.NavigationService.Navigate(new Uri("/ScanQRCodePage.xaml", UriKind.Relative));
        }

        private void radioBtn_SMS_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }

        private void radioBtn_data_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }
    }
}
